package com.eps.tesoreria.DAO;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.eps.tesoreria.Entity.DetalleFlujocajaEjecutado;
import com.eps.tesoreria.Entity.DetalleFlujocajaEjecutadoPK;
import com.eps.tesoreria.TO.ComparativoFlujoTO;
import com.eps.tesoreria.TO.FondoFijoTO;

@Repository
public class FlujoCajaHbDAO extends HibernateDaoSupport  implements IFlujoCaja {

	@Autowired
	public FlujoCajaHbDAO(SessionFactory sessionFactory) {
		super.setSessionFactory(sessionFactory);
		// TODO Auto-generated constructor stub
	}
	
	public boolean insertaFlujoProyectado(String anio, ArrayList datos)throws Exception  {
		boolean retorno = false;
		Session session = getHibernateTemplate().getSessionFactory()
				.openSession();

		Transaction tx = session.beginTransaction();
		tx.begin();
		try {
		
		for(int i=0;i< datos.size();i++){
			  ArrayList dat_list = (ArrayList)datos.get(i);
			  int id_concepto = Integer.parseInt(dat_list.get(0).toString());
			  String mes ="";
			  for(int j=3;j< dat_list.size()-1;j++){
				  switch(j){
				  	case 3:System.out.println(anio+" - "+ "01" +id_concepto+" - "+dat_list.get(j).toString());mes="01";break;
				  	case 4:System.out.println(anio+" - "+ "02" +id_concepto+" - "+dat_list.get(j).toString());mes="02";break;
				  	case 5:System.out.println(anio+" - "+ "03" +id_concepto+" - "+dat_list.get(j).toString());mes="03";break;
				  	case 6:System.out.println(anio+" - "+ "04" +id_concepto+" - "+dat_list.get(j).toString());mes="04";break;
				  	case 7:System.out.println(anio+" - "+ "05" +id_concepto+" - "+dat_list.get(j).toString());mes="05";break;
				  	case 8:System.out.println(anio+" - "+ "06" +id_concepto+" - "+dat_list.get(j).toString());mes="06";break;
				  	case 9:System.out.println(anio+" - "+ "07" +id_concepto+" - "+dat_list.get(j).toString());mes="07";break;
				  	case 10:System.out.println(anio+" - "+ "08" +id_concepto+" - "+dat_list.get(j).toString());mes="08";break;
				  	case 11:System.out.println(anio+" - "+ "09" +id_concepto+" - "+dat_list.get(j).toString());mes="09";break;
				  	case 12:System.out.println(anio+" - "+ "10" +id_concepto+" - "+dat_list.get(j).toString());mes="10";break;
				  	case 13:System.out.println(anio+" - "+ "11" +id_concepto+" - "+dat_list.get(j).toString());mes="11";break;
				  	case 14:System.out.println(anio+" - "+ "12" +id_concepto+" - "+dat_list.get(j).toString());mes="12";break;
				  }
				  DetalleFlujocajaEjecutadoPK dpk = new DetalleFlujocajaEjecutadoPK(anio.substring(2)+mes,id_concepto,'P');
				  DetalleFlujocajaEjecutado dfe = new DetalleFlujocajaEjecutado();
				  dfe.setDetalleFlujocajaEjecutadoPK(dpk);
				  dfe.setAnio(anio);
				  dfe.setMes(mes);
				  dfe.setMonto((dat_list.get(j).toString().trim().length()==0)?null:new BigDecimal( dat_list.get(j).toString()));
				  session.merge(dfe);
			  }
		  }
			session.flush();
			tx.commit();
			retorno = true;
		} catch (Exception e) {
			e.printStackTrace();
			tx.rollback();
			throw e;
		}

		return retorno;
	}
	
	public List<ComparativoFlujoTO> getComparativoFlujo(String anio, int id_concepto){
		Session session = getHibernateTemplate().getSessionFactory().openSession();
			Query consulta = session
					.createSQLQuery(
							"select * from [dbo].[fn_teso_flujocaja_comparativo_concepto]('"+anio+"',"+id_concepto+")").setResultTransformer(
							Transformers.aliasToBean(ComparativoFlujoTO.class));
		/*List<ComparativoFlujoTO> df = new ArrayList<ComparativoFlujoTO>();
		df.add(new ComparativoFlujoTO(5,"Ene",new BigDecimal("565233"),null));
		df.add(new ComparativoFlujoTO(5,"Feb",new BigDecimal("525233"),null));
		df.add(new ComparativoFlujoTO(5,"Mar",new BigDecimal("565233"),new BigDecimal("55233")));
		df.add(new ComparativoFlujoTO(5,"Abr",new BigDecimal("365233"),new BigDecimal("325233")));
		df.add(new ComparativoFlujoTO(5,"MAY",new BigDecimal("525233"),new BigDecimal("625233")));
		df.add(new ComparativoFlujoTO(5,"JUN",new BigDecimal("565233"),new BigDecimal("455233")));
		df.add(new ComparativoFlujoTO(5,"AGO",new BigDecimal("365233"),new BigDecimal("525233")));*/
			return consulta.list();/*df*/
	}

}
